package com.ruoyi.source.controller;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.source.api.domain.SrcInStore;
import com.ruoyi.source.api.domain.SrcLogistics;
import com.ruoyi.source.service.ISrcInStoreService;
import com.ruoyi.source.service.ISrcLogisticsService;
import com.ruoyi.system.api.RemoteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 物流记录Controller
 *
 * @author ruoyi
 * @date 2024-07-23
 */
@RestController
@RequestMapping("/logistics")
public class SrcLogisticsController extends BaseController {

    @Autowired
    private ISrcLogisticsService srcLogisticsService;

    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private ISrcInStoreService isrcInStoreService;
    /**
     * 查询物流记录列表
     */

    @GetMapping("/list")
    public TableDataInfo list(SrcLogistics srcLogistics)
    {
        startPage();
        if (SecurityUtils.getUsername().equals("admin")){
            return getDataTable(srcLogisticsService.selectSrcLogisticsList(srcLogistics));
        }
        srcLogistics.setUid(SecurityUtils.getUserId());
        List<SrcLogistics> list = srcLogisticsService.selectSrcLogisticsList(srcLogistics);
        System.out.println(list.toString());
        return getDataTable(list);
    }

    /**
     * 导出物流记录列表
     */
    @RequiresPermissions("source:logistics:export")
    @Log(title = "物流记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SrcLogistics srcLogistics)
    {
        List<SrcLogistics> list = srcLogisticsService.selectSrcLogisticsList(srcLogistics);
        ExcelUtil<SrcLogistics> util = new ExcelUtil<SrcLogistics>(SrcLogistics.class);
        util.exportExcel(response, list, "物流记录数据");
    }

    /**
     * 获取物流记录详细信息
     */
    @RequiresPermissions("source:logistics:query")
    @GetMapping(value = "/{logisticsId}")
    public AjaxResult getInfo(@PathVariable("logisticsId") Long logisticsId)
    {
        return success(srcLogisticsService.selectSrcLogisticsByLogisticsId(logisticsId));
    }

    /**
     * 新增物流记录
     */
    @RequiresPermissions("source:logistics:add")
    @Log(title = "物流记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SrcLogistics srcLogistics)
    {
        Long goodsId = srcLogistics.getGoodsId();
        srcLogistics.setCreateTime(new Date());

        return toAjax(srcLogisticsService.save(srcLogistics));
    }

    /**
     * 修改物流记录
     */
    //@RequiresPermissions("source:logistics:edit")
    @Log(title = "物流记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SrcLogistics srcLogistics)
    {
        if (srcLogistics.getIsFault().equals("2")){
            SrcInStore inStore = new SrcInStore();
            inStore.setStoreId(srcLogistics.getStoreId());
            inStore.setGoodsId(srcLogistics.getGoodsId());
            inStore.setInStoreNum(srcLogistics.getGoodsNum());
            inStore.setCreateTime(new Date());
            inStore.setCreateBy(SecurityUtils.getUsername());
            inStore.setGoodsType(srcLogistics.getGoodsType());
            inStore.setIsFalut("0");
            isrcInStoreService.save(inStore);
            srcLogistics.setUpdateBy(SecurityUtils.getUsername());
            srcLogistics.setEndeTime(new Date());
            return toAjax(srcLogisticsService.saveOrUpdate(srcLogistics));
        }
        srcLogistics.setCreateBy(SecurityUtils.getUsername());
        srcLogistics.setStartTime(new Date());
        return toAjax(srcLogisticsService.saveOrUpdate(srcLogistics));
    }

    /**
     * 删除物流记录
     */
    @RequiresPermissions("source:logistics:remove")
    @Log(title = "物流记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{logisticsIds}")
    public AjaxResult remove(@PathVariable Long[] logisticsIds) {
        return toAjax(srcLogisticsService.deleteSrcLogisticsByLogisticsIds(logisticsIds));
    }

    @RequestMapping("/deptUser")
    public AjaxResult getUserByDept()
    {
        return  remoteUserService.getUserByDept(110);
    }



}
